let hisId = 0,currentPage = 1, pageSize = 9;
$(function() {
    let hisHomeUrl = location.href, hisParamArr = hisHomeUrl.split('?');
    hisId = hisParamArr[1];
    hisId = hisId.split('#')[0];
    initHisInfo();
    pageHisVent(currentPage, pageSize);
    setTimeout(hisHomeAccessAddOne,800);
});

/**
 * 初始化他的信息
 */
function initHisInfo() {
    if (isNull(hisId) || hisId == 0){
        alert('该用户不存在');
        return;
    }
	$.ajax({
		type: 'post',
		url : '/ventIndex/getHisInfo',
		dataType: 'json',
        async: false,
		timeout: 120000,
		data: {'ventUserId': hisId},
		success: function (hisInfo) {
            setInfo(hisInfo);
		},
		error: function () {
			alert('他的用户信息初始化失败，请稍候再试');
		}
	});
}

/**
 * 加载吐槽数据集合
 * @param currentPage  当前面码
 * @param pageSize   每页显示记录数
 */
function pageHisVent(currentPage, pageSize) {
    let hisVentHeight = $('#hisVentList').height();
    $('.page-mask').css({'height': hisVentHeight + 'px','margin-top': -hisVentHeight + 'px'});
    $('#loadingWord').css('line-height',hisVentHeight + 'px');
    $.ajax({
        type: 'post',
        url: '/ventIndex/pageHisVent',
        dataType: 'json',
        timeout: 30000,
        data: {
            ventUserId: hisId,
            pageNo: currentPage,
            pageSize: pageSize
        },
        success: function(pageVent) {
            loadVentList(currentPage, pageSize, pageVent);
        },
        error: function() {
            $('.loading,.page-mask').hide();
            alert('系统太忙了，请稍候再试！')
        }
    });
}
/**
 * 加载吐槽数据列表
 * @param currentPage 当前页号
 * @param pageSize 每页显示的订单数
 * @param pageVent 吐槽分页数据
 */
function loadVentList(currentPage, pageSize,pageVent) {
    let ventArray = pageVent.list,ventListDiv = $('#hisVentList'),paginate = $('.wp-paginate');
    ventListDiv.children().remove();
    if (ventArray != null && ventArray.length > 0) {
        //服务订单数据
        let appendArr = [],ventListSize = ventArray.length;
        for (let s = 0; s < ventListSize; s ++) {
            appendArr.push(getVentHtml(s + 1,ventArray[s],'h'));
        }
        ventListDiv.append(appendArr.join('')).show();
        //添加分页工具栏
        paginate.children().remove();
        //总记录数,总页数
        ventTotalPage = parseInt(pageVent.pages);

        let ellipsisLi = '<li class="page-ellipsis">…</li>',
            pageNums = pageVent.navigatepageNums, aHref = 'href="javascript:void(0)"';
        let firstClass = 'first-page', previousClass = 'previous-page', isHave = '';
        //如果没有上一页
        if (!pageVent.hasPreviousPage) {
            firstClass = 'no-first';
            previousClass = 'no-previous';
            isHave = 'no-have';
        }
        let appendStr = '<li title="第一页" class="' + firstClass + '"><a ' + aHref + ' class="first ' + isHave + '"></a></li>' +
            '<li title="上一页" class="' + previousClass + '"><a ' + aHref + ' class="previous ' + isHave + '"></a></li>';
        if (pageNums[0] > 1){
            appendStr += ellipsisLi;
        }
        if (ventTotalPage > 0) {
            //页面要显示的页数集合
            for (let g = 0; g < pageNums.length; g++) {
                let currentClass = pageNums[g] == currentPage ? 'current' : 'have';
                appendStr += '<li><a ' + aHref + ' class="page ' + currentClass + '">' + pageNums[g] + '</a></li>';
            }
        }
        if (pageNums[pageNums.length - 1] < pageVent.pages){
            appendStr += ellipsisLi;
        }
        let nextClass = 'next-page', lastClass = 'last-page';
        isHave = '';
        //如果没有下一页
        if (!pageVent.hasNextPage){
            nextClass = 'no-next';
            lastClass = 'no-last';
            isHave = 'no-have';
        }
        appendStr += '<li title="下一页" class="' + nextClass + '"><a ' + aHref + ' class="next ' + isHave + '"></a></li>' +
            '<li title="最后一页" class="' + lastClass + '"><a ' + aHref + ' class="last ' + isHave + '"></a></li>';
        paginate.append(appendStr);
    } else {
        ventListDiv.append('<span style="fonts-size: 18px;">' + $('#seeNickName').text() + '很幸运，没有需要吐槽的</span>');
    }

    //分页数字点击事件
    $(".wp-paginate a").click(function() {
        let className = this.className;
        //如果class不包含no-have，并且不是当前页
        if (className.indexOf('no-have') > -1 || parseInt(this.innerText) == currentPage) {
            return;
        }
        if (className.indexOf('first') > -1) {
            currentPage = 1;
        } else if (className.indexOf('previous') > -1) {
            currentPage = currentPage > 1 ? currentPage - 1 : 1;
        } else if (className.indexOf('next') > -1) {
            currentPage = currentPage < ventTotalPage ? currentPage + 1 : ventTotalPage;
        } else if (className.indexOf('last') > -1) {
            currentPage = ventTotalPage;
        } else {
            currentPage = parseInt(this.innerText);
        }
        pageHisVent(currentPage, pageSize);
    });
    $('#hisVentTotal').text(pageVent.total);
    $('.loading,.page-mask').hide();
    let contentWidth = $('#hisVentList').width();
    //设置内容的宽度
    $('.content').css('width', (contentWidth - 96) + 'px');
}

/**
 * 主页访问数加1
 */
function hisHomeAccessAddOne() {
    let hisHomeIds = sessionStorage.getItem('accessHisHomeIds');
    hisHomeIds = hisHomeIds == null ? '' : hisHomeIds;
    //如果在sessionStorage生命周期内，已经访问过某用户的主页，则只统计一次
    if (hisHomeIds.indexOf(hisId + ',') >= 0) {
        return;
    }
    sessionStorage.setItem('accessHisHomeIds',hisHomeIds + hisId + ',');
    $.ajax({
        type: 'post',
        url : '/ventIndex/hisHomeAccessAddOne',
        dataType: 'json',
        async: true,
        data: {userId: hisId},
        success: function () {
        }
    });
}